using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;

namespace BaseSystem._System.UserControl
{
    public partial class ucRolesTree : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.trRoles.Attributes.Add("onclick", "CheckEvent()");
            }
        }

        /// <summary>
        /// Gets or sets the checked nodes.
        /// </summary>
        /// <value>The checked nodes.</value>
        public IDictionary<string, string> CheckedNodes
        {
            get
            {
                IDictionary<string, string> result = new Dictionary<string, string>();
                foreach (TreeNode trChecked in trRoles.CheckedNodes)
                {
                    if (trChecked.ChildNodes.Count >= 1 && trChecked.ChildNodes[0].Value == "true")
                    {
                        result.Add(trChecked.Value + "|" + trChecked.Parent.Value, trChecked.ChildNodes[0].Checked.ToString());

                    }
                }
                return result;
            }
            set
            {
                this.trRoles.CollapseAll();
                ClearCheckedNode(this.trRoles.Nodes);
                foreach (KeyValuePair<string, string> keyValue in value)
                {
                    string[] roleSyS = keyValue.Key.Split('|');
                    CheckParent(roleSyS[0], roleSyS[1], keyValue.Value.ToLower());
                }
            }
        }

        public TreeNodeCollection CheckedTreeViewNodes
        {
            get
            {
                return trRoles.CheckedNodes;
            }
        }
        private void ClearCheckedNode(TreeNodeCollection Nds)
        {
            foreach (TreeNode node in Nds)
            {
                node.Checked = false;
                ClearCheckedNode(node.ChildNodes);
            }
        }
        /// <summary>
        /// Checks the parent.
        /// </summary>
        /// <param name="value">The value.</param>
        private void CheckParent(string key,string value,string withGrant)
        {
            foreach (TreeNode parent in this.trRoles.Nodes)
            {
                CheckChild(parent,key, value,withGrant);
            }
        }
        /// <summary>
        /// Checks the child.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="value">The value.</param>
        private void CheckChild(TreeNode parent, string key, string value, string withGrant)
        {
            foreach (TreeNode child in parent.ChildNodes)
            {
                if (withGrant == "false")
                {
                    if (child.Value == value && child.Parent != null && child.Parent.Value == key)
                    {
                        SetParentCheck(child);
                    }
                }
                else
                {
                    if (child.Value == withGrant && child.Parent.Parent.Value == key && child.Parent.Value == value)
                    {
                        SetParentCheck(child);
                    }

                } 
                if (child.ChildNodes.Count > 0)
                {
                    CheckChild(child, key, value, withGrant);
                }
            }
        }

        /// <summary>
        /// Sets the parent check.
        /// </summary>
        /// <param name="tn">The tn.</param>
        private void SetParentCheck(TreeNode tn)
        {
            tn.Checked = true;
            if (tn.Parent != null && tn.Parent.Checked == false)
            {
                tn.Parent.Checked = true;
                if (tn.Parent.Parent != null)
                {
                    SetParentCheck(tn.Parent);
                }
            }
        }

        /// <summary>
        /// Trees the init.
        /// </summary>
        /// <param name="sysID">The sys ID.</param>
        /// <param name="sysName">Name of the sys.</param>
        public void TreeInit(string sysID, string sysName)
        {
            this.trRoles.Nodes.Clear();

            BLL.Systems systems = new BLL.Systems();
            DataTable systemsData = systems.GetSystems();
            
            foreach (DataRow dr in systemsData.Rows)
            {
                TreeNode rootNode = new TreeNode();

                rootNode.Value =  dr[SysRoles.FIELD_SysID].ToString();
                rootNode.Text = dr[SysRoles.FIELD_SysID].ToString();
                SysRoles sysRoles = new SysRoles();

                rootNode.SelectAction = TreeNodeSelectAction.Expand;
                DataTable rolesData = sysRoles.GetList(SysRoles.FIELD_SysID + "='" + dr[SysRoles.FIELD_SysID] + "'");
                this.trRoles.Nodes.Add(rootNode);
                LoadNode(rootNode, dr[SysRoles.FIELD_SysID].ToString());
            }
        } 

        /// <summary>
        /// Loads the root node.
        /// </summary>
        /// <param name="topFuncs">The top funcs.</param>
        private void LoadNode(TreeNode rootNode, string sysID/*XmlNode xmlNode*/)
        {

            TreeNode node;
            SysRoles sysRoles = new SysRoles();

            DataTable rolesData = sysRoles.GetList(SysRoles.FIELD_SysID + "='" + sysID + "'");

            foreach (DataRow drChild in rolesData.Rows)
            {
                node = new TreeNode();
                
                node.Value =drChild[SysRoles.FIELD_RoleID].ToString();
                node.Text = drChild[SysRoles.FIELD_RoleName].ToString();

                node.SelectAction = TreeNodeSelectAction.Expand;
                rootNode.ChildNodes.Add(node);

                TreeNode nodeGrant = new TreeNode();
                nodeGrant.SelectAction = TreeNodeSelectAction.Expand;
               nodeGrant.Text = "WithGrant";
               nodeGrant.Value = "true";
               node.ChildNodes.Add(nodeGrant);
            }
        }
    }
}